package com.car.sys.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.car.bussi.mapper.BusChecksMapper;
import com.car.bussi.mapper.BussiCustomerMapper;
import com.car.common.Constant;
import com.car.common.Result;
import com.car.common.base.BaseVO;
import com.car.common.base.EchartDataVO;
import com.car.sys.mapper.SysUsersMapper;
import com.car.sys.query.SysUsersQuery;
import com.car.sys.service.IStatisService;
import com.car.sys.vo.SysUsersVO;

@Service
public class StatisServiceImpl implements IStatisService {
	
	@Autowired
	private BussiCustomerMapper bussiCustomerMapper;
	
	@Autowired
	private BusChecksMapper busChecksMapper;
	
	@Autowired
	private SysUsersMapper sysUsersMapper;

	@Override
	public Result pie() {
		List<Map<String, Object>> data = bussiCustomerMapper.selectPie();
		EchartDataVO echartDataVO = new EchartDataVO();
		echartDataVO.setSeriesData(data);
		// 图例数据
		List<String> legendData = new ArrayList<String>();
		for (Map<String, Object> map : data) {
			legendData.add(map.get("name").toString());
		}
		// 设置图例的值
		echartDataVO.setLegendData(legendData);
		return new Result(echartDataVO);
	}

	@Override
	public Result line(Integer year) {
		List<Integer> data = busChecksMapper.totalMonthMoney(year);
		EchartDataVO echartDataVO = new EchartDataVO();
		echartDataVO.setSeriesData(data);
		return new Result(echartDataVO);
	}

	@Override
	public Result bar(Integer year) {
		/**
		 * 查询所有的业务员
		 * 假设每个业务员的年销售额为0
		 * 从查询的结果查找是否存在销售额，若存在则使用查询出来的销售额，没有则设置为0
		 */
		SysUsersQuery query = new SysUsersQuery();
		query.setType(Constant.USER_SALES);
		// 查询所有业务员
		List<BaseVO> list = sysUsersMapper.selectList(query);
		// X轴数据容器
		List<String> xaxisData = new ArrayList<String>();
		// 图标系列数据容器
		List<Object> seriesData = new ArrayList<Object>();
		// 业务员具体的年度销售额
		List<Map<String, Integer>> data = busChecksMapper.selectUserSaleMoney(year);
		// 将 List转化为map便与搜索
		Map<Integer, Object> map = new HashMap<Integer, Object>();
		for (Map<String, Integer> d : data) {
			Integer id = d.get("id");
			Object money = d.get("money");
			map.put(id, money);
		}
		// 循环查询业务员
		for (BaseVO vo : list) {
			SysUsersVO user = (SysUsersVO) vo;
			// 业务员名称
			String realName = user.getRealName();
			// 业务员ID
			Integer userId = user.getId();
			xaxisData.add(realName);
			// 查询的结果查找是否存在销售额，若存在则使用查询出来的销售额，没有则设置为0
			if (map.containsKey(userId)) {
				seriesData.add(map.get(userId));
			}else {
				seriesData.add(0);
			}
		}
		EchartDataVO echartDataVO = new EchartDataVO();
		echartDataVO.setXaxis(xaxisData);
		echartDataVO.setSeriesData(seriesData);
		return new Result(echartDataVO);
	}

}
